function Paint(canvas) {
	this.canvas = canvas;
	this.context = null;
	this.color = "#121212";
	this.down_x;
	this.down_y;
	this.up_x;
	this.up_y;

	this.up_x_reset=-1;
	this.up_y_reset=-1;
	this.down_x_reset=-1;
	this.down_y_reset=-1;
	
	this.r=-1;
	this.down_x_before=-1;
	this.down_y_before=-1;
}

Paint.prototype = {

	initCanvas: function() {
		this.context = this.canvas.getContext('2d');
		// 清空context
		this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
	},

	initForDrawingFreeLine: function() {

		var paint = this;

		this.context.lineWidth = 1;
		this.context.lineCap = 'round';

		this.canvas.addEventListener('mousedown', function(e) {
			paint.isMouseDown = true;
			if(pen==3){
				paint.iLastX = e.clientX - paint.canvas.offsetLeft + (window.pageXOffset||document.body.scrollLeft||document.documentElement.scrollLeft);
				paint.iLastY = e.clientY - paint.canvas.offsetTop + (window.pageYOffset||document.body.scrollTop||document.documentElement.scrollTop);
			}
			else if (pen==0)
			{
				paint.down_x=getPX(e)-paint.canvas.offsetLeft;
				paint.down_y=getPY(e)-paint.canvas.offsetTop;
			}
			else if (pen==1)
			{
				paint.down_x=getPX(e)-paint.canvas.offsetLeft;
				paint.down_y=getPY(e)-paint.canvas.offsetTop;
			}
			else if (pen==2)
			{
				paint.down_x=getPX(e)-paint.canvas.offsetLeft;
				paint.down_y=getPY(e)-paint.canvas.offsetTop;
			}
			
		});

		this.canvas.addEventListener('mouseup', function(event) {
			paint.isMouseDown = false;
			if(pen==3){
				paint.iLastX = -1;
				paint.iLastY = -1;
			}
			else if (pen==0)
			{
				paint.up_x=getPX(event)-paint.canvas.offsetLeft;
				paint.up_y=getPY(event)-paint.canvas.offsetTop;

				
				paint.down_x_reset=paint.down_x+drawArea_x/2-((paint.down_x+paint.up_x)/2);
				paint.down_y_reset=paint.down_y;
				paint.up_x_reset=paint.up_x+drawArea_x/2-((paint.down_x+paint.up_x)/2);
				paint.up_y_reset=paint.up_y;
				
				paint.context.fillStyle=paint.color;	

				paint.context.fillRect(paint.down_x_reset,paint.down_y_reset,(paint.up_x-paint.down_x),(paint.up_y-paint.down_y));
			}
			else if (pen==1)
			{
				paint.up_x=getPX(event)-paint.canvas.offsetLeft;
				paint.up_y=getPY(event)-paint.canvas.offsetTop;

				paint.down_x_reset=paint.down_x+drawArea_x/2-((paint.down_x+paint.up_x)/2);
				paint.down_y_reset=paint.down_y;
				paint.up_x_reset=paint.up_x+drawArea_x/2-((paint.down_x+paint.up_x)/2);
				paint.up_y_reset=paint.up_y;


				
				paint.context.beginPath();
				paint.context.fillStyle=paint.color;	
				paint.context.arc((paint.up_x_reset-paint.down_x_reset)/2+paint.down_x_reset, (paint.up_y_reset-paint.down_y_reset)/2+paint.down_y_reset, (paint.up_y-paint.down_y)/2, 0*Math.PI,  2*Math.PI, false);
				paint.context.closePath();
				paint.context.fill();			
			}
			else if (pen==2)
			{
				paint.up_x=getPX(event)-paint.canvas.offsetLeft;
				paint.up_y=getPY(event)-paint.canvas.offsetTop;

				paint.context.beginPath();
				paint.context.strokeStyle =paint.color;
				paint.context.moveTo(paint.down_x,paint.down_y);
				paint.context.lineTo(paint.up_x,paint.up_y);
				paint.context.closePath();
				paint.context.stroke();
			}
			deduceInk_main();
		});

		this.canvas.addEventListener('mousemove', function(event) {
			if(pen==3){
				if (paint.isMouseDown) {
					var iX = event.clientX - paint.canvas.offsetLeft + (window.pageXOffset || document.body.scrollLeft || document.documentElement.scrollLeft);
					var iY = event.clientY - paint.canvas.offsetTop + (window.pageYOffset || document.body.scrollTop || document.documentElement.scrollTop);
					paint.context.beginPath();
					paint.context.moveTo(paint.iLastX, paint.iLastY);
					paint.context.lineTo(iX, iY);
					paint.context.stroke();
					paint.iLastX = iX;
					paint.iLastY = iY;
				}
			}
			else if (pen==0)
			{
				if (paint.isMouseDown) {
					
					if((paint.down_x_before==paint.down_x)||(paint.down_y_before==paint.down_y)){
						if ((paint.up_x_reset>=paint.down_x_reset)&&(paint.up_y_reset>=paint.down_y_reset))
						{
							paint.context.clearRect(paint.down_x_reset-1,paint.down_y_reset,paint.up_x_reset-paint.down_x_reset+2,paint.up_y_reset-paint.down_y_reset);
						}
						else if((paint.up_x_reset<paint.down_x_reset)&&(paint.up_y_reset>=paint.down_y_reset)){
							paint.context.clearRect(paint.up_x_reset-1,paint.down_y_reset,paint.down_x_reset-paint.up_x_reset+2,paint.up_y_reset-paint.down_y_reset);
						}
						else if((paint.up_x_reset>=paint.down_x_reset)&&(paint.up_y_reset<paint.down_y_reset)){
							paint.context.clearRect(paint.down_x_reset-1,paint.up_y_reset,paint.up_x_reset-paint.down_x_reset+2,paint.down_y_reset-paint.up_y_reset);
						}
						else if((paint.up_x_reset<paint.down_x_reset)&&(paint.up_y_reset<paint.down_y_reset)){
							paint.context.clearRect(paint.up_x_reset-1,paint.up_y_reset,paint.down_x_reset-paint.up_x_reset+2,paint.down_y_reset-paint.up_y_reset);
						}
					}
					

					paint.up_x=getPX(event)-paint.canvas.offsetLeft;
					paint.up_y=getPY(event)-paint.canvas.offsetTop;

					
					paint.down_x_reset=paint.down_x+drawArea_x/2-((paint.down_x+paint.up_x)/2);
					paint.down_y_reset=paint.down_y;
					paint.up_x_reset=paint.up_x+drawArea_x/2-((paint.down_x+paint.up_x)/2);
					paint.up_y_reset=paint.up_y;
					
					paint.down_x_before=paint.down_x;
					paint.down_y_before=paint.down_y;

					paint.context.fillRect(paint.down_x_reset,paint.down_y_reset,(paint.up_x-paint.down_x),(paint.up_y-paint.down_y));
				}

			}
			else if (pen==1)
			{
				if (paint.isMouseDown) {
					
					paint.context.clearRect(paint.down_x_reset,paint.down_y_reset,(paint.up_x-paint.down_x),(paint.up_y-paint.down_y));
					
					if((paint.down_x_before==paint.down_x)||(paint.down_y_before==paint.down_y)){
						paint.context.clearRect(paint.down_x_reset-paint.r,paint.down_y_reset,paint.r*2+paint.r,paint.r*2);
					}
								
					paint.up_x=getPX(event)-paint.canvas.offsetLeft;
					paint.up_y=getPY(event)-paint.canvas.offsetTop;

					paint.down_x_reset=paint.down_x+drawArea_x/2-((paint.down_x+paint.up_x)/2);
					paint.down_y_reset=paint.down_y;
					paint.up_x_reset=paint.up_x+drawArea_x/2-((paint.down_x+paint.up_x)/2);
					paint.up_y_reset=paint.up_y;

					paint.r=(paint.up_y-paint.down_y)/2;
					paint.down_x_before=paint.down_x;
					paint.down_y_before=paint.down_y;
					
					paint.context.beginPath();
					paint.context.fillStyle=paint.color;	
					paint.context.arc((paint.up_x_reset-paint.down_x_reset)/2+paint.down_x_reset, (paint.up_y_reset-paint.down_y_reset)/2+paint.down_y_reset, (paint.up_y-paint.down_y)/2, 0*Math.PI,  2*Math.PI, false);
					paint.context.closePath();
					paint.context.fill();
				}
			}
			else if (pen==2)
			{
				if (paint.isMouseDown) {

					//日系 扇子风
					
					paint.up_x=getPX(event)-paint.canvas.offsetLeft;
					paint.up_y=getPY(event)-paint.canvas.offsetTop;

					paint.context.beginPath();
					paint.context.strokeStyle =paint.color;
					paint.context.moveTo(paint.down_x,paint.down_y);
					paint.context.lineTo(paint.up_x,paint.up_y);
					paint.context.closePath();
					paint.context.stroke();
				}
			}
		});
	},

	changeDrawColor: function(color) {
		this.context.strokeStyle = color;
		this.color=color;
		this.context.fillStyle=color;
	},

	changeDrawWidth: function(width) {
		this.context.lineWidth = width;
	},

	myClear: function() {
		this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
		this.up_x=-1;
		this.up_y=-1;
		this.down_x=-1;
		this.down_y=-1;
	},


};


function getPX(e){
	e=e||window.event;
	var x=e.pageX||(e.clientX+(document.documentElement.scrollLeft||document.body.scrollLeft));
	return x;
}
//获取鼠标位置Y坐标
function getPY(e){
	e=e||window.event;
	var y=e.pageY||(e.clientY+(document.documentElement.scrollTop||document.body.scrollTop));
	return y;
}